//Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2006-6-29 19:31:44
//Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
//Decompiler options: packimports(3) 
//Source File Name:   InetAddressWatchdog.java

package ants.p2p.utils.addresses;

import java.io.*;
import java.net.InetAddress;
import java.util.ArrayList;

//Referenced classes of package ants.p2p.utils.addresses:
//         InetAddressRange

public class InetAddressWatchdog
{

 public static InetAddressWatchdog getInstance()
 {
     if(instance == null)
         return instance = new InetAddressWatchdog();
     else
         return instance;
 }

 public void loadFilterFile(String fileName)
 {
     addresses = new ArrayList();
     try
     {
         File file = new File(fileName);
         if(!file.exists())
             return;
         FileReader fr = new FileReader(file);
         BufferedReader br = new BufferedReader(fr);
         String currentLine = null;
         do
         {
             if((currentLine = br.readLine()) == null)
                 break;
             if(currentLine.charAt(0) != '#')
             {
                 String initAddress[] = currentLine.split("[-]");
                 String finalAddress[] = initAddress[1].split("[,]");
                 addresses.add(new InetAddressRange(initAddress[0], finalAddress[0]));
             }
         } while(true);
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
 }

 InetAddressWatchdog()
 {
     addresses = new ArrayList();
 }

 public boolean allowedAddress(String ip)
     throws Exception
 {
     ip = InetAddress.getByName(ip).getHostAddress();
     for(int x = 0; x < addresses.size(); x++)
     {
         InetAddressRange iar = (InetAddressRange)addresses.get(x);
         if(iar.isInRange(ip))
             return false;
     }

     return true;
 }

 static InetAddressWatchdog instance = null;
 ArrayList addresses;

}